package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Msg;
import com.pojo.Table;
import com.service.TableService;
import com.shiro.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//@RequiresPermissions(value={"foodTable:*"})
@Controller
@ResponseBody
public class TableController extends BaseController {
    @Autowired
    TableService tableService;

    @RequestMapping("table/all")
    public Msg getAll () {
        List<Table> tables = tableService.getAll();
        return Msg.success().add("tables", tables);
    }

    @RequestMapping("table/{size}")
    public Msg getTableBySize (@PathVariable("size") Integer size) {
//        Integer s = Integer.parseInt(size);
        List<Table> tables = tableService.getBySize(size);
        return Msg.success().add("tables", tables);
    }

    @RequestMapping("table/{status}")
    public Msg getTableByStatus (@PathVariable("status") String status) {
        List<Table> tables = tableService.getByStatus(status);
        return Msg.success().add("tables", tables);
    }

    @RequestMapping(value="/table",method= RequestMethod.POST)
    public Msg addTable (@RequestBody Table table) {
        tableService.addTable(table);
        return Msg.success();
    }

    @RequestMapping(value="/table/{tId}",method= RequestMethod.DELETE)
    public Msg deleteTable (@PathVariable("tId") String tId){
        Integer id = Integer.parseInt(tId);
        tableService.deleteTable(id);
        return Msg.success();
    }

    //分页获取全部数据,self
    @RequestMapping(value = "table/page", method = RequestMethod.GET)
    @ResponseBody
    public Msg getTableWithPage(@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 10);

        List<Table> tableList = tableService.getAll();

        PageInfo page = new PageInfo(tableList, 10);
        Msg msg = Msg.success().add("pageInfo", page);
        return msg;
    }

    //根据两个条件进行模糊分页查询
    @RequestMapping(value = "table/find", method = RequestMethod.POST)
    @ResponseBody
    public Msg getTableByQuery(@RequestParam(value = "tStatus",defaultValue = "") String tStatus, @RequestParam(value="tSize", defaultValue = "0") String tSize ,@RequestParam(value = "pn", defaultValue = "1") Integer pn){
        PageHelper.startPage(pn, 10);



        System.out.println(tStatus+"状态-----"+tSize + "-----大小");

        List<Table> tableList = tableService.getTableByTwoCondition(tStatus, Integer.parseInt(tSize));

        PageInfo page = new PageInfo(tableList, 10);
        Msg msg = Msg.success().add("pageInfo", page);

        return msg;
    }

    //更新/修改餐桌信息
    @RequestMapping(value = "/table/{tId}",method = RequestMethod.PUT)
    @ResponseBody
    public Msg updateTable(@RequestBody Table tablevo){
//        if(tableService.checkTableByTName(tablevo.gettName())){
//            tableService.updateTable(tablevo);
//            return Msg.success();
//
//        }else{
//            System.out.println(tablevo.gettName());
//            return Msg.fail().add("errorMessage","餐桌名重复");
//        }
        tableService.updateTable(tablevo);
        return Msg.success();


    }


    //得到空座（没有人）的餐桌
    @RequestMapping(value = "table/part", method = RequestMethod.GET)
    public Msg getTableByStatus() {
        List<Table> tables = tableService.selectTableWithStatus();
        return Msg.success().add("tables", tables);
    }



}
